package com.academic.system.service.impl;

import com.academic.common.constant.HttpStatus;
import com.academic.common.core.domain.entity.SysBook;
import com.academic.common.core.domain.entity.SysUsers;
import com.academic.common.core.page.TableDataInfo;
import com.academic.system.domain.rep.ReadBookRep;
import com.academic.system.domain.req.NewReq;
import com.academic.system.mapper.SysUsersMapper;
import com.academic.system.mapper.dao.SysBookDao;
import com.academic.system.service.SysBookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (SysBook)表服务实现类
 *
 * @author makejava
 * @since 2024-10-31 22:55:00
 */
@Service("sysBookService")
@Component
public class SysBookServiceImpl extends ServiceImpl<SysBookDao, SysBook> implements SysBookService {
    private final SysUsersMapper sysUsersMapper;
    private final SysBookDao sysBookDao;


    public SysBookServiceImpl(SysUsersMapper sysUsersMapper, SysBookDao sysBookDao) {
        this.sysUsersMapper = sysUsersMapper;
        this.sysBookDao = sysBookDao;
    }

    @Override
    public TableDataInfo getBookHistory(NewReq newReq) {
        // 获取用户的阅读历史
        PageHelper.startPage(newReq.getPageNum(), newReq.getPageSize());
        List<ReadBookRep> bookReps = new ArrayList<>();
        QueryWrapper<SysBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", newReq.getId());
        List<SysBook> sysBooks = sysBookDao.selectList(queryWrapper);
        PageInfo<SysBook> pageInfo = new PageInfo<>(sysBooks);
        QueryWrapper<SysUsers> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id", newReq.getId());
        SysUsers sysUser = sysUsersMapper.selectOne(queryWrapper1);

        if (sysBooks != null && !sysBooks.isEmpty()) {
            for (SysBook sysBook : sysBooks) {
                ReadBookRep bookRep = new ReadBookRep();
                bookRep.setBookName(sysBook.getBookName());
                bookRep.setStatus(sysBook.getStatus());
                bookRep.setCreateTime(sysBook.getCreateTime());
                bookRep.setUserId(newReq.getId());
                bookRep.setStudentName(sysUser.getNickName());
                bookRep.setPhone(sysUser.getPhonenumber());
                bookReps.add(bookRep);
            }
        }
        return getDataTable(bookReps,pageInfo.getTotal());
    }
//    @SuppressWarnings({ "rawtypes", "unchecked" })
    public TableDataInfo getDataTable(List<?> list,Long total)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }
}

